from base import Session
import memcache
import json
SECTION = 'memcache_session'
class MemcacheSession(Session):
    def config(self, cfg):
        self.cfg = cfg
        hosts_num =  int(cfg.get(SECTION, 'hosts'))
        hosts = []
        for i in range(1, hosts_num+1):
            hosts.append(cfg.get(SECTION, 'host' + str(i)))
        self.hosts = hosts
        
    def build_session(self):
        #self.stores = {}
        #return
        self.cacheid = "session" + self.sid
        self.mc = memcache.Client(self.hosts)
        js = self.mc.get(self.cacheid);
        if js != None:
            self.stores = json.loads(js)
        
    def commit(self):
        js = json.dumps(self.stores)
        self.mc.set(self.cacheid, js)